package common.jdbc.sql;


import java.util.HashMap;
import java.util.Map;

/**
 * @作者： caopengfei
 * @时间： 2020/9/7
 */
public class SqlAdapterFactory {

    static Map<String,DBType> dbTypeMap = new HashMap<>();


    static{
        //内置目前支持的数据库类型
        dbTypeMap.put("SQLSERVER",DBType.SQLSERVER);
        dbTypeMap.put("ORACLE",DBType.ORACLE);
        dbTypeMap.put("MYSQL",DBType.MYSQL);
        dbTypeMap.put("GREENPLUM",DBType.GREENPLUM);
        dbTypeMap.put("POSTGRESQL",DBType.POSTGRESQL);
        dbTypeMap.put("DAMENG",DBType.DAMENG);
        dbTypeMap.put("HSQL",DBType.HSQL);
    }


    /**
    * @comment： 获取SQL适配器
    * @author： caopengfei
    * @time：  2020/9/8 10:04
    */
    public static SqlAdapter getSqlAdapter(String dbTypeName){
        DBType dbType  = dbTypeMap.get(dbTypeName.toUpperCase());
        if(dbType == null){
           throw new RuntimeException(dbTypeName+":不合法");
        }
        try {
            Class clazz = Class.forName(dbType.clazz());
            return (SqlAdapter)clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(dbTypeName+":不合法，没有配置实现类");
        }
    }

    public static DBType getDBTypeByName(String dbTypeName){
        DBType dbType  = dbTypeMap.get(dbTypeName);
        if(dbType == null){
            throw new RuntimeException(dbTypeName+":不合法");
        }
        return dbType;
    }
}
